package private

import (
	"dc-go/global"
	"dc-go/model"
	"dc-go/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ApiSqlparam struct{}

func (a *ApiSqlparam) GetSqlparamList(c *gin.Context) {
	var requestSqlparamList model.RequestGetSqlparamList
	if err := model.RequestShouldBindJSON(c, &requestSqlparamList); err != nil {
		return
	}
	if sqlParamList, total, err := servicePrivate.ServiceSqlparam.GetSqlparamList(requestSqlparamList); err != nil {
		global.DcLogger.Error("获取Sql参数列表失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("获取Sql参数列表失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessData(model.ResponsePage{
			Records:  sqlParamList,
			Page:     requestSqlparamList.Page,
			PageSize: requestSqlparamList.PageSize,
			Total:    total,
		}, c)
	}
}

func (a *ApiSqlparam) EditSqlparam(c *gin.Context) {
	var toEditSqlparam model.RequestEditSqlparam
	if err := model.RequestShouldBindJSON(c, &toEditSqlparam); err != nil {
		return
	}
	if err := servicePrivate.ServiceSqlparam.EditSqlparam(toEditSqlparam, utils.GetEmpeeAcct(c)); err != nil {
		global.DcLogger.Error("编辑Sql参数失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("编辑Sql参数失败, "+err.Error(), c)
	} else {
		global.DcLogger.Warn(utils.GetEmpeeAcct(c) + "编辑Sql参数成功! ")
		model.ResponseSuccessMessage("编辑Sql参数成功! ", c)
	}
}

func (a *ApiSqlparam) AddSqlparam(c *gin.Context) {
	var toAddSqlparam model.RequestAddSqlparam
	if err := model.RequestShouldBindJSON(c, &toAddSqlparam); err != nil {
		return
	}
	if err := servicePrivate.ServiceSqlparam.AddSqlparam(toAddSqlparam, utils.GetEmpeeAcct(c)); err != nil {
		global.DcLogger.Error("添加Sql参数失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("添加Sql参数失败, "+err.Error(), c)
	} else {
		global.DcLogger.Warn(utils.GetEmpeeAcct(c) + "添加Sql参数成功! ")
		model.ResponseSuccessMessage("添加Sq参数l成功! ", c)
	}
}

func (a *ApiSqlparam) QuerySqlparamById(c *gin.Context) {
	var toQueryId model.RequestQueryById
	if err := model.RequestShouldBindJSON(c, &toQueryId); err != nil {
		return
	}
	if sqlparam, err := servicePrivate.ServiceSqlparam.QuerySqlparamById(toQueryId.Id); err != nil {
		global.DcLogger.Error("查找Sql参数失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("查找Sql参数失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessageData(gin.H{"records": &sqlparam}, "查找Sql参数成功! ", c)
	}
}
func (a *ApiSqlparam) QuerySqlparamBySqlparamCode(c *gin.Context) {
	var toQuerySqlparamCode model.RequestQueryBySqlparamCode
	if err := model.RequestShouldBindJSON(c, &toQuerySqlparamCode); err != nil {
		return
	}
	if sqlparam, err := servicePrivate.ServiceSqlparam.QuerySqlparamBySqlparamCode(toQuerySqlparamCode.SqlparamCode); err != nil {
		global.DcLogger.Error("查找Sql参数失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("查找Sql参数失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessageData(gin.H{"records": sqlparam}, "查找Sql参数成功! ", c)
	}
}
func (a *ApiSqlparam) QuerySqlparamOptionsBySqlparamCode(c *gin.Context) {
	var toQuerySqlparamCode model.RequestQueryBySqlparamCode
	if err := model.RequestShouldBindJSON(c, &toQuerySqlparamCode); err != nil {
		return
	}
	if sqlparamOptions, err := servicePrivate.ServiceSqlparam.QuerySqlparamOptionsBySqlparamCode(toQuerySqlparamCode.SqlparamCode, toQuerySqlparamCode.Constraint); err != nil {
		global.DcLogger.Error("查找Sql参数取值范围失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("查找Sql参数取值范围失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessageData(gin.H{"records": sqlparamOptions}, "查找Sql参数取值范围成功! ", c)
	}
}
func (a *ApiSqlparam) DeleteSqlparamById(c *gin.Context) {
	var toDeleteId model.RequestQueryById
	if err := model.RequestShouldBindJSON(c, &toDeleteId); err != nil {
		return
	}
	if err := servicePrivate.ServiceSqlparam.DeleteSqlparamById(toDeleteId.Id); err != nil {
		global.DcLogger.Error("删除Sql参数失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("删除Sql参数失败, "+err.Error(), c)
	} else {
		global.DcLogger.Warn(utils.GetEmpeeAcct(c) + "删除Sql参数成功! ")
		model.ResponseSuccessMessage("删除Sql参数成功! ", c)
	}
}

func (a *ApiSqlparam) ValidateVariable(c *gin.Context) {
	var toTestVariable model.RequestValidateVariable
	if err := model.RequestShouldBindJSON(c, &toTestVariable); err != nil {
		return
	}
	res, err := servicePrivate.ServiceSqlparam.ValidateVariable(toTestVariable.VariableString)
	if err != nil {
		global.DcLogger.Error("测试变量失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("测试变量失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessageData(gin.H{"records": res}, "测试变量成功! ", c)
	}
}

func (a *ApiSqlparam) ValidateSqlparamOptionSql(c *gin.Context) {
	var toTestSql model.RequestValidateSql
	if err := model.RequestShouldBindJSON(c, &toTestSql); err != nil {
		return
	}
	res, err := servicePrivate.ServiceSqlparam.ValidateSqlparamOptionSql(toTestSql.SqlString)
	if err != nil {
		global.DcLogger.Error("测试参数选项Sql失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("测试参数选项Sql失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessageData(gin.H{"records": res}, "测试参数选项Sql成功! ", c)
	}
}
